package com.contract.management.util;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class StrUtils {
	
	private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	
	private static final String[] HAN_DIV_STR = new String[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
	/**
	 * 首字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String ucFirst(String str) {
		if(isEmpty(str)) return str;
		char[] buf = str.toCharArray();
		buf[0] -= 32;
		return String.valueOf(buf);
	}
	
	/**
	 * 判断字符串是否为null或空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || "".equals(str.trim());
	}
	
	/**
	 * 将以其他连接符连接的字符串改成驼峰表示  <br />
	 * 如: 	toCamel(first_name, "_") => "firstName"
	 * 		toCamel("hello.world", "\\.") => "firstName"
	 * @param str			
	 * @param separator	连接符
	 * @return
	 */
	public static String toCamel(String str, String separator) {
		if(isEmpty(str)) return str;
		String[] arr = str.split(separator);
		if(arr.length <= 1) return str;
		
		StringBuilder sb = new StringBuilder(arr[0]);
		for(int i = 1; i <arr.length; i++) {
			sb.append(ucFirst(arr[i]));
		}
		return sb.toString();
	}
	
	/**
	 * 获取字符串长度，汉字为2，英文为1
	 * @param s
	 * @return
	 */
	public static int getLen(String s) {  
		int len = 0;    
		String chinese = "[\u4e00-\u9fa5]";    
		// 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1    
		for (int i = 0; i < s.length(); i++) {    
			// 获取一个字符    
			String temp = s.substring(i, i + 1);    
			// 判断是否为中文字符    
			if (temp.matches(chinese)) { 
				len += 2;    
			} else {
				len += 1;    
			}    
		}    
		//进位取整    
		return len;    
	}
	
	/**
	 * 后边添加空格到指定长度，汉字长度为2
	 * @param s
	 * @param len
	 * @return
	 */
	public static String addBlank(Object val, int len) {
		String s = null;
		if(val == null) {
			s = "";
		} else {
			s = String.valueOf(val).trim();
		}
		int l = getLen(s);
		if(l >= len) return s;
		return String.format("%-" + (len - (l - s.length())) + "s", s);
	}
	
	public static String addRBlank(Object val, int len) {
		String s = null;
		if(val == null) {
			s = "";
		} else {
			s = String.valueOf(val).trim();
		}
		int l = getLen(s);
		if(l >= len) return s;
		return String.format("%" + (len - (l - s.length())) + "s", s);
	}
	/**
	 * 转换成大写数字
	 * 
	 * @param num
	 * @return 
	 */
	public static List<String> toUpperMoneyList(Integer num) {
		if(num == null) num = 0;
		List<String> list = new ArrayList<>();
		while(num >= 10) {
			int cur = num % 10;
			num = num / 10;
			list.add(CN_UPPER_NUMBER[cur]);
		}
		list.add(CN_UPPER_NUMBER[num]);
		return list;
	}
	
	public static String getUuidName(String fileName) {
		String suffixName = fileName.substring(fileName.lastIndexOf("."));
		String uuid = UUID.randomUUID().toString();
		return uuid + suffixName;
	}
	
	/**
	 * 人民币金额大写
	 * @param je
	 * @return
	 */
	public static String toRmbDx(Integer je) {
		return toRmbDx(je, 6);
	}
	public static String toRmbDx(Integer je, int length) {
		if(je == null || je == 0) return addBlank("", length);
		
		String numStr = String.valueOf(je);
	    String rmbStr = "";
	    boolean lastzero = false;
	    boolean hasvalue = false; // 亿、万进位前有数值标记
	    int len, n;
	    len = numStr.length();
	    if (len > 15)
	      return "数值过大!";
	    for (int i = len - 1; i >= 0; i--) {
	      if (numStr.charAt(len - i - 1) == ' ')
	        continue;
	      n = numStr.charAt(len - i - 1) - '0';
	      if (n < 0 || n > 9)
	        return "输入含非数字字符!";

	      if (n != 0) {
	        if (lastzero)
	          rmbStr += CN_UPPER_NUMBER[0]; // 若干零后若跟非零值，只显示一个零
	        // 除了亿万前的零不带到后面
	        // if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) ) //
	        // 如十进位前有零也不发壹音用此行
	        if (!(n == 1 && (i % 4) == 1 && i == len - 1)) // 十进位处于第一位不发壹音
	          rmbStr += CN_UPPER_NUMBER[n];
	        rmbStr += HAN_DIV_STR[i]; // 非零值后加进位，个位为空
	        hasvalue = true; // 置万进位前有值标记

	      } else {
	        if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) // 亿万之间必须有非零值方显示万
	          rmbStr += HAN_DIV_STR[i]; // “亿”或“万”
	      }
	      if (i % 8 == 0)
	        hasvalue = false; // 万进位前有值标记逢亿复位
	      lastzero = (n == 0) && (i % 4 != 0);
	    }

	    if (rmbStr.length() == 0)
	      return CN_UPPER_NUMBER[0]; // 输入空字符或"0"，返回"零"
	    return addBlank(rmbStr, length);
	  }
	
}
